﻿using System;
using System.Collections.Generic;

using Caliburn.Micro;

using Crucell.ProductDatabase.Client.Specs.Common;

using Rhino.Mocks.Interfaces;

namespace Client.Specs.Support
{
    public static class Extensions
    {
        public static CoroutineEnumerator GetCoroutineEnumerator(this IEnumerable<IResult> coroutines)
        {
            return new CoroutineEnumerator(coroutines);
        }

        public static IMethodOptions<object> Invoke<T>(this IMethodOptions<object> methodOptions, int argumentIndex, T data) where T : class
        {
            return methodOptions.WhenCalled(mi =>
            {
                if (argumentIndex == Argument.Last)
                {
                    argumentIndex = mi.Arguments.Length - 1;
                }

                var callback = mi.Arguments[argumentIndex] as Action<T>;
                if (callback == null)
                {
                    throw new InvalidOperationException("The specified argument is not of type Action<" + typeof (T).Name + ">");
                }

                callback(data);
            });
        }
    }

    public static class Argument
    {
        public const int Last = int.MaxValue;
    }
}